import random

# 定义麻将牌
suits = ['万', '条', '筒']
ranks = [str(i) for i in range(1, 10)]
winds = ['东', '南', '西', '北']
dragons = ['中', '发', '白']

# 生成一副麻将牌
def generate_mahjong_tiles():
    tiles = []
    # 万字、条子、筒子牌
    for suit in suits:
        for rank in ranks:
            for _ in range(4):
                tiles.append(rank + suit)
    # 风牌
    for wind in winds:
        for _ in range(4):
            tiles.append(wind)
    # 箭牌
    for dragon in dragons:
        for _ in range(4):
            tiles.append(dragon)
    return tiles

# 洗牌
def shuffle_tiles(tiles):
    random.shuffle(tiles)
    return tiles

# 发牌
def deal_tiles(tiles):
    players = [[] for _ in range(4)]
    # 每人先发 13 张牌
    for _ in range(13):
        for i in range(4):
            players[i].append(tiles.pop())
    return players

# 摸牌
def draw_tile(tiles, player_hand):
    if tiles:
        tile = tiles.pop()
        player_hand.append(tile)
        return tile
    return None

# 出牌
def discard_tile(player_hand, tile_index):
    if 0 <= tile_index < len(player_hand):
        return player_hand.pop(tile_index)
    return None

# 显示玩家手牌
def show_player_hand(player_hand):
    print("你的手牌:", player_hand)

if __name__ == "__main__":
    # 生成并洗牌
    tiles = generate_mahjong_tiles()
    shuffled_tiles = shuffle_tiles(tiles)

    # 发牌
    players = deal_tiles(shuffled_tiles)

    current_player = 0
    while shuffled_tiles:
        # 显示当前玩家手牌
        show_player_hand(players[current_player])

        # 摸牌
        drawn_tile = draw_tile(shuffled_tiles, players[current_player])
        if drawn_tile:
            print(f"你摸到了: {drawn_tile}")
            show_player_hand(players[current_player])

            # 出牌
            while True:
                try:
                    tile_index = int(input("请输入要出的牌的索引（从 0 开始）: "))
                    discarded_tile = discard_tile(players[current_player], tile_index)
                    if discarded_tile:
                        print(f"你出了: {discarded_tile}")
                        break
                    else:
                        print("无效的索引，请重新输入。")
                except ValueError:
                    print("输入无效，请输入一个整数。")

        # 切换到下一个玩家
        current_player = (current_player + 1) % 4

    print("牌已摸完，游戏结束。")